

String[] lines;
int index = 1;
float [][] rawpoints = new float[5000][3];//Multidimensional array to store raw plots
float [][] fixpoints = new float[200][3];//Multidimensional array to store fixation points
float [][] density = new float[1280][1024];//Multidimensional array for the heatmap
//int fixRange = 15;// This variable decides the fixation time we are considering: Every 5 points in the data correspond to rough 100ms

int fixDist = 12;// The distance around each point inside which the certain amounts of points should fall to be considered as a fixation.
boolean fixFlag = true;
 int counter = 0;
int k = 0;//To store the number of fixation points
 boolean flag = true;
 int rawcounter = 0;
 
 float avgFixDist = 0;
float avgFixTime = 0;

void setup() {
  size(1280, 1024);
  background(255);// The graph images can be added instead
 // stroke(0);
  frameRate(30);
  lines = loadStrings("stimulus_22.txt");// A csv file with X, Y and time points
  
  int j = 0;
//  float []temp = {0,0};
//  print(temp[0]+","+temp[1]+"\n");
   //--------------------------------------
  /*Code for making array of the raw eye tracker points*/
 //-------------------------------------
  
  for (int i=0;i<lines.length;i++)
  {
    String[] pieces = split(lines[i], '\t');
   // print(pieces[0]+", "+pieces[1]+"\n");
    if (pieces.length == 7) {
      rawpoints[i][0] = float(pieces[0]);
      rawpoints[i][1] = float(pieces[2]);
      rawpoints[i][2] = float(pieces[4]);
      //print(rawpoints[i][0]+", "+rawpoints[i][1]+"\n");
      rawcounter = rawcounter+1;
    }
  }
//print(rawcounter+"\n");
 //--------------------------------------
  /*Code for getting fixation points out of raw points*/
 //-------------------------------------

  while(index<rawcounter)
  {
     j = index;
      while(fixFlag)//(j<index+fixRange+1)
      {
        
       // print(abs(rawpoints[j][0]-rawpoints[index][0])+", "+abs(rawpoints[j][1]-rawpoints[index][1])+", "+j+"\n");
          if(dist(rawpoints[j+1][0],rawpoints[j+1][1],rawpoints[j][0],rawpoints[j][1])<fixDist)//(abs(rawpoints[j][0]-rawpoints[index][0])<fixDist)&&(abs(rawpoints[j][1]-rawpoints[index][1])<fixDist))
          {
            fixFlag = true;
            rawpoints[j+1][0]=(rawpoints[j+1][0]+rawpoints[j][0])/2;
            rawpoints[j+1][1]=(rawpoints[j+1][1]+rawpoints[j][1])/2;
            j = j+1;
          }
          else
          {
            fixFlag = false;
            
          }
      }
      if((rawpoints[j][2]-rawpoints[index][2])>0.1&&(j-index>4)&&((rawpoints[j][2]-rawpoints[index][2])<0.41))//only consider fixations group with atleast 5 points and fixation time of 100ms and less than 400ms.
      {
        fixpoints[k][0] = rawpoints[j][0];
        fixpoints[k][1] = rawpoints[j][1];
        fixpoints[k][2] = rawpoints[j][2]-rawpoints[index][2];
        
         //print(fixpoints[k][0]+", "+fixpoints[k][1]+", "+j+"\n");
        k = k+1;
        print(index+"\t"+(rawpoints[j][2]-rawpoints[index][2])+"\t\t"+(j-index)+"\n");
        index = j;
       
      }
      else
      {
       // print(index+"\n");
        index = index + 1;
        
        fixFlag = true;
       // print("did we get here\n");
      }
  }
  /////////////////////////////////////////////////
  print(k+"\n");//Th number of fixation points - this can be changes by changing the fixDist and fixRange variables
  /////////////////////////////////////////////////////
  
  ////////////////////////////////////////////////////
  /////////////////////////////////////////////////////
  
  
    //--------------------------------------
  /*Code for finding avg distance between fixation points*/ 
 //-------------------------------------

 float []distance = new float[k-1];
 
  for(int i = 0; i<k-1;i++)
  {
    distance[i] = dist(fixpoints[i+1][0],fixpoints[i+1][1],fixpoints[i][0], fixpoints[i][1]);
   // print(distance[i]+"\n");
  }
  
  float totalDist = 0;
  
  for(int i = 0; i<k-1;i++)
  {
    totalDist = totalDist+distance[i];
    avgFixDist = totalDist/(k-1);
  }
 ////////////////////////////////////////////////////////// 
  print(avgFixDist+"\n");
  ////////////////////////////////////////////////////////
  
  
  /////////////////////////////////////////////////////////////
    //--------------------------------------
  /*Code for finding avg fixation time*/ 
 //-------------------------------------
 /////////////////////////////////////////////////////////
  float totalTime = 0;
  for(int i = 0; i<k;i++)
  {
    totalTime = totalTime+fixpoints[i][2];
    avgFixTime = totalTime/k;
  }
  print(avgFixTime+"\n");
  
  
  
  
  //////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////
  

   //--------------------------------------
  /*Code for assigning a specific density to every point on the screen for ultimately drawing the heatmap*/ 
 //-------------------------------------

  for(int x = 0; x<1280; x++)
  {
      for(int y=0; y<1024; y++)
      {
        for(int z = 0; z<k; z++)
        {
          if(((fixpoints[z][0]-x)*(fixpoints[z][0]-x))+((fixpoints[z][1]-y)*(fixpoints[z][1]-y))<(fixDist*fixDist))
          {
          density[x][y]=density[x][y]+1;
        
          }
         // if(density[x][y]!=0)
          //print(density[x][y]);
        }
      }
  }
  
  //--------------------------------------
  /*Code for max density - it came out  to be ---44---- which would be the worst case scenario since this heatmap contains data from 12 stimuli.
  The max for only one stimuli will be far less*/
 //-------------------------------------
  
//  int maxDensity = 0;
//  for(int x = 0; x<1280; x++)
//  {
//      for(int y=0; y<1024; y++)
//      {
//        if(density[x][y]>maxDensity)
//        maxDensity = density[x][y];
//        
//      }
//  }
//  
//  print(maxDensity);
}



//void draw() {
//
////  if (counter < k) {
////    
////      ellipse(fixpoints[counter][0], fixpoints[counter][1],25,25);
////    }
////   
////    counter = counter + 1;
//if(flag)
//  {
//  for(int x = 0; x<1280; x++)
//    {
//        for(int y=0; y<1024; y++)
//        {
//          
//          // DEciding the color range for the heatmaps
//          // For now I have just made it grayscale as its difficult for me to assess how many point each stimuli would have
//          // But i think with each stimuli there would be a lot less points and the variables can be manipulated accordingly
//          stroke(255-(ceil(density[x][y])*25));//dividing densities ranging from 0-44 into 15 groups and shading accordingly
//            
//            point(x,y);
//          
//           
//          }
//      
//    }
//  print("done");
//  }
//  flag = false;
//}
